/*测试数据*/
var testdata={
		id: 1,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	};

$(function(){
	getDataMes();
	$("#btnSaveEdit").on('click',function(){
		submitData();
	});
	$("#btnReback").on('click',function(){
		var tgid=getUrlParam("tgid");
		if(tgid!=null && tgid!=""){
			history.go(-1);
		}else{
			$("#editForm")[0].reset();
		}
	});
});
/*修改数据时，获取数据并显示*/
function getDataMes(){
	var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid!=""){
		
		/*if(testdata.id!=null){
			$("#manageContent").attr({"tgid":testdata.id});
		}
		if(testdata.phone!=null){
			$("#memberMobile").val(testdata.phone);
		}
		if(testdata.realName!=null){
			$("#realName").val(testdata.realName);
		}
		if(testdata.address!=null){
			$("#address").val(testdata.address);
		}
		if(testdata.shoppingAddress!=null){
			$("#shoppingAddress").val(testdata.shoppingAddress);
		}*/
        $.post(
            gobalServer+"user/selectOne",
            {"userId":tgid},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.object;
                    if(datarow.userId!=null){
                        $("#manageContent").attr({"tgid":datarow.userId});
                    }
                    if(datarow.phone!=null){
                        $("#memberMobile").val(datarow.phone);
                    }
                    if(datarow.realName!=null){
                        $("#realName").val(datarow.realName);
                    }
                    if(datarow.address!=null){
                        $("#address").val(datarow.address);
                    }
                    if(datarow.shoppingAddress!=null){
                        $("#shoppingAddress").val(datarow.shoppingAddress);
                    }
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );

	}
};
/*新增或修改数据*/
function submitData(){
	var tgid=$("#manageContent").attr("tgid");
	var memberMobile=$("#memberMobile").val().trim();
	var realName=$("#realName").val().trim();
	var address=$("#address").val().trim();
	var shoppingAddress=$("#shoppingAddress").val().trim();
	if(memberMobile==null || memberMobile==""){
		consoleMes($("#manageContent"),2000,"warning","手机不能为空！");
		return;
	}
	if(!ischeckmobile(memberMobile)){
		consoleMes($("#manageContent"),2000,"warning","手机格式不正确！");
		return;
	}
	if(realName==null || realName==""){
		consoleMes($("#manageContent"),2000,"warning","请输入真实姓名！");
		return;
	}
	if(tgid!=null && tgid!=""){
		if(getUrlParam("tgid")!=null){
			/*consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");//success、info、warning、danger
        	setTimeout(function () {
		    	window.location.reload();
		    }, 2500);*/
            $.post(
                gobalServer+"user/update",
                {"userId":tgid,"phone":memberMobile,"realName":realName,"address":address,"shoppingAddress":shoppingAddress},
                function(backData){
                    if(backData.statusCode==200){
                        consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");//success、info、warning、danger
                        setTimeout(function () {
                            window.location.reload();
                        }, 2500);
                    }else{
                        consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                    }
                }
            );

	    }else{
			consoleMes($("#manageContent"),2000,"warning","无法提交，请刷新页面重试！");
		}
	}else{
		/*consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
    	setTimeout(function () {
	    	window.location.href=gobalWeb+"user/usershow.html";
	    }, 2500);*/
        $.post(
            gobalServer+"user/insert",
            {"phone":memberMobile,"realName":realName,"address":address,"shoppingAddress":shoppingAddress},
            function(backData){
                if(backData.statusCode==200){
                    consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
                    setTimeout(function () {
                        window.location.href=gobalWeb+"user/usershow.html";
                    }, 2500);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );

	}
		
};
